package com.bysc.executor.handler;

import com.bysc.executor.client.CmsClient;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.Set;

import static com.bysc.commonutils.constant.RedisConstant.*;

@Component
public class FileDelectJobHandler {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CmsClient cmsClient;

    /**
     * minio删除垃圾文件
     */
    @XxlJob("FileDelectJob")
    public void FileDelectJob() throws Exception {
        System.out.println("FileDelectJob执行了.... " + LocalDateTime.now());
        Set<String> difference = redisTemplate.opsForSet().difference(FILE_URL_DB_RESOURCES,FILE_URL_RESOURCES);
        if(!ObjectUtils.isEmpty(difference)) {
            for (String diffUrl : difference) {
                String[] split = diffUrl.split("/");
//            http://localhost:9005/study/kun.pdf
                //http://42.194.233.38:9000/banner/9705c64d-c830-4d19-8f1b-d27a3df95ee7-1320622.jpeg
                String bucketName = split[3];
                String objectName = split[4];
                cmsClient.deleteFile(bucketName,objectName);
            }
        }

        System.out.println("FileDelectJob执行结束了.... " + LocalDateTime.now());
    }
}
